package com.p2p.auth.controller.api.inside;


import com.p2p.auth.controller.Routes;
import com.p2p.auth.domain.entity.User;
import com.p2p.auth.service.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.annotations.common.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * Created by lipei on 2017/4/18.
 */

@RestController
@RequestMapping
@Api(value = "用户API", description = "用户的CRUD操作，注意必传参数与参数")
public class InsideUserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserRepository userRepository;


    @ApiOperation(value = "根据手机号获取用户id", notes = "根据手机号获取用户id", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.INSIDE_USER_ID_BY_PHONENUM, method = RequestMethod.POST)
    public String getUserIdByPhoneNum(@RequestBody String userPhoneNum) {
        String userId = null;
        User User = userRepository.findByPhoneNum(userPhoneNum);

        if(User != null){
            userId = User.getUserId();
        }

        return userId;
    }

    @ApiOperation(value = "根据手机号获取用户名称", notes = "根据手机号获取用户名称", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.INSIDE_USER_NAME_BY_PHONENUM, method = RequestMethod.POST)
    public String getUserNameByPhoneNum(@RequestBody String userPhoneNum) {
        String userName =  null;
        User User = userRepository.findByPhoneNum(userPhoneNum);

        if(User != null && StringHelper.isNotEmpty(User.getRealName())){
            userName = User.getRealName();
            logger.info("user is exist  userPhoneNum = {}, userName = {}", userPhoneNum,  userName);
        }else{
            logger.error("user not eixt userPhoneNum = {} ", userName);
        }

        return userName;
    }

    @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.INSIDE_USER_GET_MSG_BY_PHONENUM, method =  RequestMethod.POST)
    public HashMap getUserMsgByPhoneNum(@RequestBody String phoneNum) {
        logger.info("in request phoneNum = {}", phoneNum);
        User user = userRepository.findByPhoneNum(phoneNum);

        return getMsg(user);
    }

    @ApiOperation(value = "根据userId获取用户信息", notes = "根据id获取用户信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.INSIDE_USER_GET_MSG_BY_USERID, method =  RequestMethod.POST)
    public HashMap getUserMsgByUserId(@RequestBody String userId) {
        logger.info("in request userId = {}", userId);
        User user = userRepository.findByUserId(userId);

        return getMsg(user);
    }

    public HashMap<String, Object> getMsg(User user){
        if(user == null){
            return null;
        }
        HashMap<String, Object> result = new HashMap<>(5);
        result.put("userId", "");
        result.put("phoneNum", "");
        result.put("realName", "");
        result.put("retailersId", "");

        if(user != null){
            result.put("userId", user.getUserId());
            result.put("phoneNum", user.getPhoneNum());
            result.put("realName", user.getRealName());
            result.put("retailersId", user.getRetailersId());
        }
        return  result;
    }
}
